package com.ddf.norman.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: UserLoginAuthenticationFailureHandler.java
 * @Description: 登录失败后的处理
 * @author: norman
 * @date:  2020-06-11 19:36
 * @version V1.0
 */
@Component
public class UserLoginAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> map = new HashMap<>(16);
        map.put("status", 401);
        if (e instanceof LockedException) {
            map.put("msg", "账户被锁定 登录失败");
        } else if (e instanceof UsernameNotFoundException) {
            map.put("msg", "账号不存在");
        } else if (e instanceof BadCredentialsException) {
            map.put("msg", "账户名或密码错误");
        } else if (e instanceof DisabledException) {
            map.put("msg", "账户被禁用 登录失败");
        } else if (e instanceof AccountExpiredException) {
            map.put("msg", "账户已过期 登录失败");
        } else if (e instanceof CredentialsExpiredException) {
            map.put("msg", "密码已过期 登录失败");
        } else {
            map.put("msg", "登录失败,请检查您的网络");
        }
        ObjectMapper om = new ObjectMapper();
        out.write(om.writeValueAsString(map));
        out.flush();
        out.close();
    }
}
